package com.example.zyfypt516wh.activities;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.universalvideoview.UniversalMediaController;
import com.universalvideoview.UniversalVideoView;
import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.bean.CollectResult;
import com.example.zyfypt516wh.bean.FocusResult;
import com.example.zyfypt516wh.bean.ResBean;
import com.example.zyfypt516wh.common.Common;
import com.example.zyfypt516wh.iface.CollectionListener;
import com.example.zyfypt516wh.iface.DetailListener;
import com.example.zyfypt516wh.iface.FocusListener;
import com.example.zyfypt516wh.model.CollectionModel;
import com.example.zyfypt516wh.model.DetailModel;
import com.example.zyfypt516wh.model.FocusModel;

import java.util.List;

public class VideoActivity extends AppCompatActivity {


    View mVideoLayout;
    UniversalVideoView mVideoView;
    UniversalMediaController mMediaController;

    private TextView tvCollect;
    private TextView tvFollow;

    private SharedPreferences sp;
    String type, id,userid,sessionID,collecStatus,followStatus;
    String focusType="userfocus";

    // String url="https://amicool.neusoft.edu.cn/Uploads/video/video/zjs-general/git-guide.mp4";
    String url="";

    private DetailListener listener = new DetailListener() {
        @Override
        public void onResponse(ResBean bean) {
            url=bean.getVideopath();
            mVideoView.setVideoPath(Common.VIDEOURL+url);
            mVideoView.start();

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(VideoActivity.this, "失败： " + msg, Toast.LENGTH_SHORT).show();
            System.out.println("--" + msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);


        sp=this.getSharedPreferences("user",MODE_PRIVATE);
        sessionID=sp.getString("sessionID","");


        type = getIntent().getStringExtra("type");
        id = getIntent().getStringExtra("id");
        userid=getIntent().getStringExtra("userid");

        DetailModel model=new DetailModel();
        model.getDetail("video",id,sessionID,listener);
        initView();
        //判断是否收藏
        existCollection();

        //判断是否关注
        existFollow();

        //收藏
        tvCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (collecStatus.equals("1")){//状态1 尚未收藏
                    toCollection();
                }else if (collecStatus.equals("0")){//状态0 已收藏--》取消收藏
                    unCollection();
                }


            }
        });
        //关注
        tvFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (followStatus.equals("1")){//状态1 未关注
                    toFollow();
                }else if (followStatus.equals("0")){//状态0 已关注--》取消关注
                    unFollow();
                }

            }
        });
    }



    FocusListener existFListener =new FocusListener() {
        @Override
        public void onResponse(List<FocusResult> list) {

        }

        @Override
        public void onResponse(String res) {
            followStatus = res;
            if (followStatus.equals("1")) {
                tvFollow.setText("关注");
            } else if (followStatus.equals("0")) {
                tvFollow.setText("取消关注");
            }
        }

        @Override
        public void onFail(String msg) {

        }

    };

    /**
     * 判断是否关注
     */
    private void existFollow() {
        FocusModel model=new FocusModel();
        model.exist(focusType,Integer.parseInt(userid),sessionID,existFListener);
    }


    FocusListener focusListener =new FocusListener() {
        @Override
        public void onResponse(List<FocusResult> list) {

        }

        @Override
        public void onResponse(String res) {
            Log.i("TAG", "关注结果onResponse:---》 "+res);
            existFollow();

        }

        @Override
        public void onFail(String msg) {
            System.out.println("判断是否关注请求--->"+msg);
        }
    };

    /**
     * 关注
     */
    private void toFollow() {
        FocusModel model=new FocusModel();
        model.focus(focusType,Integer.parseInt(userid),sessionID, focusListener);
    }

    /**
     * 取消关注
     */
    private void unFollow() {
        FocusModel model=new FocusModel();
        model.unfocus(focusType,Integer.parseInt(userid),sessionID, focusListener);
    }

    CollectionListener existListener =new CollectionListener() {
        @Override
        public void onResponse(String res) {
            collecStatus=res;
            if (collecStatus.equals("1")){
                tvCollect.setText("收藏");
            }else if (collecStatus.equals("0")){
                tvCollect.setText("取消收藏");
            }
        }
        @Override
        public void onResponse(List<CollectResult> list) { }
        @Override
        public void onFail(String msg) {
            System.out.println("判断是否存在请求--->"+msg);
        }
    };

    //判断是否收藏网络请求
    private void existCollection() {
        CollectionModel model=new CollectionModel();
        model.exist(type,Integer.parseInt(id),sessionID,existListener);
    }

    CollectionListener collectionListener =new CollectionListener() {
        @Override
        public void onResponse(String res) {
            Log.i("TAG", "收藏结果onResponse:---》 "+res);
            existCollection();

        }
        @Override
        public void onResponse(List<CollectResult> list) { }
        @Override
        public void onFail(String msg) {
            System.out.println("判断是否存在请求--->"+msg);
        }
    };
    //收藏网络请求
    private void toCollection() {
        CollectionModel model=new CollectionModel();
        model.collect(type,Integer.parseInt(id),sessionID,collectionListener);
    }

    //取消收藏网络请求
    private void unCollection() {
        CollectionModel model=new CollectionModel();
        model.uncollect(type,Integer.parseInt(id),sessionID,collectionListener);
    }


    private void initView() {

        mVideoView = (UniversalVideoView) findViewById(R.id.videoView);
        mMediaController = (UniversalMediaController) findViewById(R.id.media_controller);
        mVideoLayout=findViewById(R.id.video_layout);
        mVideoView.setMediaController(mMediaController);

        tvCollect = findViewById(R.id.title_collection);
        tvFollow = findViewById(R.id.title_follow);

        ViewGroup.LayoutParams layoutParams = mVideoLayout.getLayoutParams();
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
        mVideoLayout.setLayoutParams(layoutParams);


        mVideoView.setVideoViewCallback(new UniversalVideoView.VideoViewCallback() {
            @Override
            public void onScaleChange(boolean isFullscreen) {
            }
            @Override
            public void onPause(MediaPlayer mediaPlayer) { // Video pause
                Log.d("TAG", "onPause UniversalVideoView callback");
            }
            @Override
            public void onStart(MediaPlayer mediaPlayer) { // Video start/resume to play
                mediaPlayer.start();
                Log.d("TAG", "onStart UniversalVideoView callback");
            }
            @Override
            public void onBufferingStart(MediaPlayer mediaPlayer) {// steam start loading
                Log.d("TAG", "onBufferingStart UniversalVideoView callback");
            }
            @Override
            public void onBufferingEnd(MediaPlayer mediaPlayer) {// steam end loading
                Log.d("TAG", "onBufferingEnd UniversalVideoView callback");
            }

        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVideoView.closePlayer();
    }
}